﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace GroupChat2013.Common
{
    /// <summary>
    /// 文件管理工具栏，用来上传下载文件使用
    /// </summary>
    public class FileManage
    {
        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="url">下载地址</param>
        /// <param name="savePath">保存路径</param>
        /// <param name="callBack">回调委托</param>
        public static void SaveFileForUrl(string url, string savePath, Action callBack)
        {
            try
            {
                AsyncClient.AsyncCall(() =>
                                {
                                    WebRequest request = WebRequest.Create(url);
                                    WebResponse response = request.GetResponse();
                                    Stream reader = response.GetResponseStream();
                                    FileStream writer = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
                                    byte[] buff = new byte[512];
                                    int c = 0; //实际读取的字节数
                                    while ((c = reader.Read(buff, 0, buff.Length)) > 0)
                                    {
                                        writer.Write(buff, 0, c);
                                    }
                                    writer.Close();
                                    writer.Dispose();
                                    reader.Close();
                                    reader.Dispose();
                                    response.Close();
                                },
                                callBack);
            }
            catch { }
        }

        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="url">下载地址</param>
        /// <param name="savePath">报错路径</param>
        public static string SaveFileForUrl(string url, string savePath)
        {
            try
            {
                WebRequest request = WebRequest.Create(url);
                WebResponse response = request.GetResponse();
                Stream reader = response.GetResponseStream();
                FileStream writer = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
                byte[] buff = new byte[512];
                int c = 0; //实际读取的字节数
                while ((c = reader.Read(buff, 0, buff.Length)) > 0)
                {
                    writer.Write(buff, 0, c);
                }
                writer.Close();
                writer.Dispose();
                reader.Close();
                reader.Dispose();
                response.Close();

                return savePath;
            }
            catch { return string.Empty; }
        }

        /// <summary>
        /// 下载保存图片
        /// </summary>
        /// <param name="url">下载路径</param>
        /// <param name="savePath">保存路径</param>
        /// <param name="callBack">回调委托</param>
        public static void SaveFileSimpleForUrl(string url, string savePath, Action callBack)
        {
            try
            {
                AsyncClient.AsyncCall(() =>
                        {
                            HttpProc.WebClient mywebclient = new HttpProc.WebClient();
                            mywebclient.DownloadFile(url, savePath);
                        },
                        callBack);
            }
            catch { }
        }

        /// <summary>
        /// 本地图片另存为
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="savePath">保存路径</param>
        public static void SaveFileForLocal(string filePath, string savePath)
        {
            try
            {
                using (FileStream stream = File.OpenRead(filePath))
                {
                    using (FileStream fileStream = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        byte[] buff = new byte[512];
                        int c = 0; //实际读取的字节数
                        while ((c = stream.Read(buff, 0, buff.Length)) > 0)
                        {
                            fileStream.Write(buff, 0, c);
                        }
                    }
                }
            }
            catch { }
        }

        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="files">文件集合</param>
        /// <param name="names">文件名字集合</param>
        /// <param name="fileDic">文件夹路径</param>
        public static void MoveImg(List<string> files, List<string> names, string fileDic)
        {
            if (!Directory.Exists(fileDic))
            {
                Directory.CreateDirectory(fileDic);
            }

            DirectoryInfo dicInfo = new DirectoryInfo(fileDic);
            var fileInfos = MethodEx.ToList(dicInfo.GetFiles());
            int index = 0;
            foreach (string file in files)
            {
                if (!File.Exists(file))
                    continue;

                FileInfo finfo = new FileInfo(file);
                bool isExist = fileInfos.Exists(x => { return x.Name.Equals(finfo.Name); });

                if (!isExist)
                {
                    FileManage.SaveFileForLocal(file, Path.Combine(fileDic, names[index]));
                }
                index++;
            }
        }
    }
}
